package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.ReturnDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.ReturnService;
import com.atguigu.jxc.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: dongdong lau
 * @CreateTime: 2021/7/24 10:36
 * @Description: 退货出库接口实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ReturnServiceImpl implements ReturnService {

    @Autowired
    private ReturnDao returnDao;

    /**
     * 保存退货单减库存
     * @param returnList
     * @param returnListGoodsStr
     * @return
     */
    @Override
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr) {
        // 对参数进行判空
        if (returnList == null && StringUtil.isEmpty(returnListGoodsStr)){
            return new ServiceVO<ErrorCode>(ErrorCode.NULL_POINTER_CODE, ErrorCode.NULL_POINTER_MESS);
        }
        // 保存退货出库列表
        returnDao.saveReturnList(returnList);

        // 对returnListGoodsStr进行转换，转换为ReturnListGoods
        returnListGoodsStr = returnListGoodsStr.replace("[", "").replace("]", "");
        ReturnListGoods returnListGoods = JSONObject.parseObject(returnListGoodsStr, ReturnListGoods.class);

        //将returnList中的主键id设置进入returnListGoodsStr
        Integer returnListId = returnList.getReturnListId();
        returnListGoods.setReturnListId(returnListId);

        // 保存退货出库商品列表
        returnDao.saveReturnListGoods(returnListGoods);

        // 减少商品库存
        Integer goodsId = returnListGoods.getGoodsId();
        Integer goodsNum = returnListGoods.getGoodsNum();
        returnDao.reduceGoods(goodsId, goodsNum);

        return new ServiceVO<SuccessCode>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /**
     * 退货单列表展示
     * @param returnNumber
     * @param supplierId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Map<String, Object> map = new HashMap<>();
        List<ReturnList> list = returnDao.list(returnNumber, supplierId, state, sTime, eTime);
        map.put("rows", list);
        return map;
    }

    /**
     * 查询商品信息
     * @param returnListId
     * @return
     */
    @Override
    public Map<String, Object> getReturnListGoodsById(Integer returnListId) {
        // 初始化返回列表
        Map<String, Object> map = new HashMap<>();
        // 对传入的id进行判空
        if(returnListId == null){
            throw new RuntimeException("参数不能为空");
        }
        // 调用接口获取数据
        List<ReturnListGoods> returnListGoodsById = returnDao.getReturnListGoodsById(returnListId);
        // 设置返回参数
        map.put("rows", returnListGoodsById);
        // 返回数据
        return map;
    }

    /**
     * 根据returnListId删除进货单信息
     * @param returnListId
     * @return
     */
    @Override
    public ServiceVO<SuccessCode> deleteReturnListById(Integer returnListId) {
        // 调用删除mapper层接口
        Integer result = returnDao.deleteReturnListById(returnListId);
        return new ServiceVO<SuccessCode>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
